<?php


namespace app\app\controller;

use app\app\model\Order;
use app\common\controller\Homebase;
use app\common\library\DataUtils;
use EasyWeChat\Factory;
use think\Db;
use app\app\model\Shop as ShopModel;

class Member extends Baseapp
{
    protected $token;
    protected $memberInfo;
    protected $memberId;
    protected $salesperson_id;
    protected $salesperson_info;

    protected function initialize()
    {
        parent::initialize();
        $authorization = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
        if (empty($authorization)) {
            responseJson(-100, '请先登录');
        }
        $token = trim($authorization);
        $member = Db::name('member')->where('access_token', '=', $token)->find();
        if (!$member) {
            responseJson(-200, '登录信息有误');
        }
        $salesperson = Db::name('salesperson')->where('member_id', '=', $member['id'])->find();
        if (!$salesperson) {
            msg(-300, '员工账号不存在', '');
        }
        if ($salesperson['status'] != 1) {
            msg(-400, '员工账号异常', '');
        }

        $this->token = $token;
        $this->memberInfo = $member;
        $this->memberId = $member['id'];

        $this->salesperson_info = $salesperson;
        $this->salesperson_id = $salesperson['id'];
    }

    //获取账号信息
    public function getMemberInfo()
    {
        $return_data = $this->appEncrypt($this->memberInfo);
        msg(0, '查询成功', $return_data);
    }

    //用户信息
    public function getSalespersonInfo()
    {
        //员工推广码 上线输入路径打开
        if (!$this->salesperson_info['tg_img_url']) {
            //todo 获取相对应的路径
            $code_url = qrcode($this->salesperson_info['tg_no'], 'pages/login/register');
            Db::name('salesperson')->update([
                'id' => $this->salesperson_id,
                'tg_img_url' => $code_url
            ]);
            $this->salesperson_info['tg_img_url'] = $code_url;
        }

        $return_data = $this->appEncrypt($this->salesperson_info);
        msg(0, '查询成功', $return_data);
    }

    //数据统计1
    public function indexData1()
    {
        $date1 = getTimestamp(0);
        $date2 = getTimestamp();
        $date3 = monthStrtotime();

        $shop_num1 = Db::name('shop')->where([
            ['salesperson_id', '=', $this->salesperson_id],
            ['is_del', '=', 0],
            ['create_time', 'between', [$date1[0], $date1[1]]]
        ])->count();

        $shop_num2 = Db::name('shop')->where([
            ['salesperson_id', '=', $this->salesperson_id],
            ['is_del', '=', 0],
            ['create_time', 'between', [$date2[0], $date2[1]]]
        ])->count();

        $shop_num3 = Db::name('shop')->where([
            ['salesperson_id', '=', $this->salesperson_id],
            ['is_del', '=', 0],
            ['create_time', 'between', [$date3[0], $date3[1]]]
        ])->count();

        $shop_num4 = Db::name('shop')->where([
            ['salesperson_id', '=', $this->salesperson_id],
            ['is_del', '=', 0],
        ])->count();

        $time_in=DataUtils::getRecentDaysRange();
        $shop_list_ids=Db::name('order')->where([
            ['create_time','between',[$time_in['start_timestamp'],$time_in['end_timestamp']]]
        ])->column('shop_id');
        $where[] = ['salesperson_id', '=', $this->salesperson_info['id']];
        $where[] = ['id', 'not in', $shop_list_ids];
        $where[] = ['status', 'in', [1,2]];
        $shop_num5=Db::name('shop')->where($where)->count();

        $return_data['a'] = $shop_num1;
        $return_data['b'] = $shop_num2;
        $return_data['c'] = $shop_num3;
        $return_data['d'] = $shop_num4;
        $return_data['e'] = $shop_num5;

        $return_data = $this->appEncrypt($return_data);
        msg(0, '查询完成', $return_data, '');
    }

    //下属门店开卡统计
    public function indexData2()
    {
        $date1 = getTimestamp(0);
        $date2 = getTimestamp();
        $date3 = monthStrtotime();

        //获取下属所有门店id
        $shop_ids = Db::name('shop')->where([
            ['salesperson_id', '=', $this->salesperson_id],
            ['is_del', '=', 0],
        ])->column('id');

        $num1 = Db::name('order')->where([
            ['shop_id', 'in', $shop_ids],
            ['create_time', 'between', [$date1[0], $date1[1]]]
        ])->count();
        $num2 = Db::name('order')->where([
            ['shop_id', 'in', $shop_ids],
            ['create_time', 'between', [$date2[0], $date2[1]]]
        ])->count();
        $num3 = Db::name('order')->where([
            ['shop_id', 'in', $shop_ids],
            ['create_time', 'between', [$date3[0], $date3[1]]]
        ])->count();

        $return_data['a'] = $num1;
        $return_data['b'] = $num2;
        $return_data['c'] = $num3;
        $return_data = $this->appEncrypt($return_data);
        msg(0, '查询完成', $return_data, '');
    }

    //门店排名
    public function indexShopList()
    {
        //获取下属所有门店id
        $shop_ids = Db::name('shop')->where([
            ['salesperson_id', '=', $this->salesperson_id],
            ['is_del', '=', 0],
            ['status', '=', 1],
        ])->column('id');

        $shopList = Db::name('shop')
            ->alias('s')
            ->where([
                ['s.id', 'in', $shop_ids],
            ])
            ->field('s.*, COUNT(o.id) as order_count')
            ->leftJoin('order o', 's.id = o.shop_id')
            ->group('s.id')
            ->order('order_count desc')
            ->select();
        foreach ($shopList as $k => $v) {
            $gh = Db::name('shop_gh')->where([
                ['shop_id', '=', $v['id']]
            ])->column('gh');
            $gh = implode(',', $gh);
            $shopList[$k]['gh'] = $gh;
        }
        $return_data = $this->appEncrypt($shopList);
        msg(0, '查询完成', $return_data, '');
    }

    //开卡订单
    public function orderList()
    {
        $data = $this->request->param();

        $data = $this->appDecrypt($data);

        $page = isset($data['page']) && $data['page'] > 0 ? (int)$data['page'] : 1;
        $length = isset($data['limit']) && $data['limit'] > 0 ? (int)$data['limit'] : 20;

        //获取下属所有门店id
        $shop_ids = Db::name('shop')->where([
            ['salesperson_id', '=', $this->salesperson_id],
            ['is_del', '=', 0],
        ])->column('id');

        $where[] = ['shop_id', 'in', $shop_ids];
        //1待审核 2已审核 3未通过
        $type = $data['type'];
        //status  审核状态 0待审核 1已审核 2审核失败
        if ($type == 1) {
            $where[] = ['status', '=', 0];
        }
        if ($type == 2) {
            $where[] = ['status', '=', 1];
        }
        if ($type == 3) {
            $where[] = ['status', '=', 2];
        }

        if (isset($data['sno']) && $data['sno']) {
            $where[] = ['phone|sim', 'like', '%' . $data['sno']];
        }
        if (isset($data['rq1']) && isset($data['rq2'])) {
            $timestamp1 = strtotime($data['rq1']);
            $timestamp2 = strtotime($data['rq2']);
            $where[] = ['create_time', 'between', [$timestamp1, $timestamp2]];
        }
        $data_list = Order::where($where)
            ->page($page, $length)
            ->order('create_time desc')
            ->paginate(
                $length, false, [
                    'page' => $page
                ]
            )->each(function ($item) {
                $package=Db::name('package')->find($item['package_id']);
                $item['package_name'] = $package['name'];

                $shop_info = Db::name('shop')->find($item['shop_id']);
                $item['shop_info'] = $shop_info;
                return $item;
            });
        $return_data = $this->appEncrypt($data_list);
        msg(0, '查询成功', $return_data);
    }

    //订单详情
    public function orderDetail()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        $order_detail = Order::where('id', '=', $data['id'])->find();

        $package_info = Db::name('package')
            ->where('id', '=', $order_detail['package_id'])
            ->find();
        $shop_info = Db::name('shop')
            ->where('id', '=', $order_detail['shop_id'])
            ->find();
        $order_detail['package_info'] = $package_info;
        $order_detail['shop_info'] = $shop_info;

        //订单流程数组
        //0 待审核 1已审核 2已拒绝
        $li_array = array(
            [
                'text' => '订单提交',
                'create_time' => $order_detail['create_time'],
                'status' => 1,
                'refuse' => '',
            ],
        );

        if ($order_detail['kf_status'] != 0) {
            $li_array[] = array(
                'text' => '客服审核',
                'create_time' => $order_detail['kf_status_time'],
                'status' => $order_detail['kf_status'],
                'refuse' => $order_detail['kf_refuse'],
            );
        }else{
            $li_array[] = array(
                'text' => '客服审核中',
                'create_time' => '',
                'status' => $order_detail['kf_status'],
                'refuse' => '',
            );
        }
        if ($order_detail['kf_status']==1){
            if ($order_detail['cw_status'] != 0) {
                $li_array[] = array(
                    'text' => '财务审核',
                    'create_time' => $order_detail['cw_status_time'],
                    'status' => $order_detail['cw_status'],
                    'refuse' => $order_detail['cw_refuse'],
                );
            }else{
                $li_array[] = array(
                    'text' => '财务审核中',
                    'create_time' => '',
                    'status' => $order_detail['cw_status'],
                    'refuse' => '',
                );
            }
        }


        $order_detail['li_array'] = $li_array;

        $order_detail = $this->appEncrypt($order_detail);
        msg(0, '查询成功', $order_detail);
    }

    //下属门店列表
    public function shopList()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        //sort_type 1默认排序 2开卡倒序 3开卡正序 4库存倒序 5库存正序
        switch ($data['sort_type']) {
            case 1:
                $sort = 'create_time desc';
                break;
            case 2:
                $sort = 'order_num desc';
                break;
            case 3:
                $sort = 'order_num ';
                break;
            case 4:
                $sort = 'bk_num desc ';
                break;
            case 5:
                $sort = 'bk_num ';
                break;
            default:
                $sort = 'create_time desc';
        }
        if (isset($data['shop_name']) && $data['shop_name']) {
            $where[] = ['shop_name', 'like', '%' . $data['shop_name'] . '%'];
        }

        $page = isset($data['page']) && $data['page'] > 0 ? (int)$data['page'] : 1;
        $length = isset($data['limit']) && $data['limit'] > 0 ? (int)$data['limit'] : 20;

        //todo 开卡 关联 Shop的 order_no
        $where[] = ['salesperson_id', '=', $this->salesperson_info['id']];
        $data_list =ShopModel::where($where)
            ->order($sort)
            ->paginate(
                $length, false, [
                    'page' => $page
                ]
            )->each(function ($item) {
                //工号
                $gh = Db::name('shop_gh')->where([
                    ['shop_id', '=', $item['id']]
                ])->column('gh');
                $gh = implode(',', $gh);
                $item['gh'] = $gh;
                return $item;
            });
        $return_data = $this->appEncrypt($data_list);
        msg(0, '查询成功', $return_data);
    }

    //店铺信息
    public function shopInfo()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);

        $shop_info = ShopModel::where('id', '=', $data['id'])
            ->find();
        $gh = Db::name('shop_gh')->where([
            ['shop_id', '=', $shop_info['id']]
        ])->column('gh');
        $gh = implode(',', $gh);
        $shop_info['gh'] = $gh;

        $return_data = $this->appEncrypt($shop_info);
        msg(0, '查询成功', $return_data);
    }

    //店铺订单记录
    public function shopOrderList()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        $page = isset($data['page']) && $data['page'] > 0 ? (int)$data['page'] : 1;
        $length = isset($data['limit']) && $data['limit'] > 0 ? (int)$data['limit'] : 20;

        $where[] = ['shop_id', '=', $data['shop_id']];
        if (isset($data['phone'])&&$data['phone']){
            $where[] = ['phone', 'like', '%'.$data['phone'].'%'];
        }
//        if (isset($data['rq1']) && isset($data['rq2'])) {
//            $timestamp1 = strtotime($data['rq1']);
//            $timestamp2 = strtotime($data['rq2']);
//            $where[] = ['create_time', 'between', [$timestamp1, $timestamp2]];
//        }
        if (isset($data['datetimerange']) && $data['datetimerange']) {
            $data['datetimerange'] = explode(',', $data['datetimerange']);
            $where[] = ['create_time', 'between', [strtotime($data['datetimerange'][0] . '00:00:00'), strtotime($data['datetimerange'][1] . '23:59:59')]];
        }

        $type = $data['type'];
        //type    1待审核 2已审核 3未通过
        //status  审核状态 0待审核 1已审核 2审核失败
        if ($type == 1) {
            $where[] = ['status', '=', 0];
        }
        if ($type == 2) {
            $where[] = ['status', '=', 1];
        }
        if ($type == 3) {
            $where[] = ['status', '=', 2];
        }
        $data_list = Order::where($where)
            ->page($page, $length)
            ->order('create_time desc')
            ->paginate(
                $length, false, [
                    'page' => $page
                ]
            )->each(function ($item) {
                $shop_info = Db::name('shop')->find($item['shop_id']);
                $item['shop_info'] = $shop_info;
                return $item;
            });
        $return_data = $this->appEncrypt($data_list);
        msg(0, '查询成功', $return_data);
    }

    //开卡数量 白卡库存
    public function kbDate()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
//        $order_num = Db::name('order')
//            ->where([
//                ['shop_id', '=', $data['shop_id']]
//            ])
//            ->count();
//        $board_num = Db::name('board')->where([
//            ['is_del', '=', 0],
//            ['shop_id', '=', $data['shop_id']],
//            ['is_bk', '=', 1],
//            ['hb_time', '>', 1],
//        ])->count();
        $shop_info = Db::name('shop')->find($data['shop_id']);
        $return_data['a'] = $shop_info['order_num'];
        $return_data['b'] = $shop_info['bk_num'];
        $return_data = $this->appEncrypt($return_data);
        msg(0, '查询成功', $return_data);
    }

    //门店审核列表
    public function shopShList()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        $page = isset($data['page']) && $data['page'] > 0 ? (int)$data['page'] : 1;
        $length = isset($data['limit']) && $data['limit'] > 0 ? (int)$data['limit'] : 20;

        $where[] = ['salesperson_id', '=', $this->salesperson_id];
        //type 0全部 1审核中 2审核通过 3已拒绝
        //status     0待审核 1审核通过 2账户关闭  3审核失败
        $type = $data['type'];
        if ($type == 0) {
            $where[] = ['status', 'in', [0, 1, 3]];
        }
        if ($type == 1) {
            $where[] = ['status', 'in', [0]];
        }
        if ($type == 2) {
            $where[] = ['status', '=', 1];
        }
        if ($type == 3) {
            $where[] = ['status', '=', 3];
        }
        $data_list = ShopModel::where($where)
            ->page($page, $length)
            ->order('create_time desc')
            ->paginate(
                $length, false, [
                    'page' => $page
                ]
            )->each(function ($item) {
                return $item;
            });
        $return_data = $this->appEncrypt($data_list);
        msg(0, '查询成功', $return_data);
    }

    //门店审核详情
    public function shopShDetail()
    {
        $data = $this->request->param();
        $shop_info = Db::name('shop')->where('id', '=', $data['shop_id'])->find();
        $shop_info['create_time'] = date('Y-m-d H:i:s', $shop_info['create_time']);
        if ($shop_info['status_time']) {
            $shop_info['status_time'] = date('Y-m-d H:i:s', $shop_info['status_time']);
        }
        $li_array = array(
            [
                'text' => '创建提交',
                'create_time' => $shop_info['create_time'],
                'status' => 1,
                'refuse' => '',
            ],
        );

        if ($shop_info['status'] != 0) {
            $li_array[] = array(
                'text' => '客服审核',
                'create_time' => $shop_info['status_time'],
                'status' => $shop_info['status'],
                'refuse' => $shop_info['reason_refusal'],
            );
        }

        $shop_info['li_array'] = $li_array;
        $return_data = $this->appEncrypt($shop_info);
        msg(0, '查询成功', $return_data);
    }

    //账户中心
    public function accountMember()
    {
        //可提现
        $money = $this->salesperson_info['money'];
        //待入账 

        $dai_money = Db::name('order')->where([
            ['salesperson_id', '=', $this->salesperson_id],
            ['status', '=', 0],
        ])->value('reward2 + commission2');
        if (!$dai_money) {
            $dai_money = 0;
        }
        //提现中
        //1业务员到账 2商家到账 3业务提现 4商家提现 5业务扣款 6商家扣款 7业务经理到账
        $txing_money = Db::name('member_finance')->where([
            ['salesperson_id', '=', $this->salesperson_id],
            ['type', '=', 3],
            ['is_count', '=', 2],
            ['is_kuan', '=', 1]
        ])->sum('dz_money');
        //累计提现
        $tx_money = Db::name('member_finance')->where([
            ['salesperson_id', '=', $this->salesperson_id],
            ['type', '=', 3],
            ['is_count', '=', 2],
            ['is_kuan', '=', 2]
        ])->sum('dz_money');

        $return_data['a'] = $money;
        $return_data['b'] = $dai_money;
        $return_data['c'] = $txing_money;
        $return_data['d'] = $tx_money;
        $return_data = $this->appEncrypt($return_data);
        msg(0, '查询成功', $return_data);

    }

    //共提现金额
    public function txMoneyMember()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);

        $rq = $data['rq']; //2025-09
        $riqi = getMonthTimestampRange($rq);

        $tx_money = Db::name('member_finance')->where([
            ['salesperson_id', '=', $this->salesperson_id],
            ['create_time', 'between', [$riqi['start_timestamp'], $riqi['end_timestamp']]],
            ['type', '=', 3],
            ['is_count', '=', 2],
        ])->sum('money');

        $return_data = $this->appEncrypt($tx_money);
        msg(0, '查询成功', $return_data);
    }

    //提现记录
    public function txList()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        //1业务员到账 2商家到账 3业务提现 4商家提现 5业务扣款 6商家扣款 7业务经理到账
        $where[] = ['salesperson_id', '=', $this->salesperson_id];
        $where[] = ['type', '=', 3];

        $rq = $data['rq']; //2025-09
        $riqi = getMonthTimestampRange($rq);

        $where[] = ['create_time', 'between', [$riqi['start_timestamp'], $riqi['end_timestamp']]];

        $page = isset($data['page']) && $data['page'] > 0 ? (int)$data['page'] : 1;
        $length = isset($data['limit']) && $data['limit'] > 0 ? (int)$data['limit'] : 20;

        $type = $data['type']; //1线上提现 2线下提现
        if ($type == 1) {
            $where[] = ['tx_type', '=', 1];
        }
        if ($type == 2) {
            $where[] = ['tx_type', '=', 2];
        }
        $data_list = Db::name('member_finance')
            ->where($where)
            ->page($page, $length)
            ->order('create_time desc')
            ->paginate(
                $length, false, [
                    'page' => $page
                ]
            )->each(function ($item) {
                return $item;
            });
        $tx_money = Db::name('member_finance')->where([
            ['salesperson_id', '=', $this->salesperson_id],
            ['create_time', 'between', [$riqi['start_timestamp'], $riqi['end_timestamp']]],
            ['type', '=', 3],
            ['is_count', '=', 2],
        ])->sum('money');

        $return_data['data'] = $data_list;
        $return_data['tx_money'] = $tx_money;

        $return_data = $this->appEncrypt($return_data);
        msg(0, '查询成功', $return_data);
    }

    //当月已提现金额
    public function nowTx2()
    {
        //type 1业务员到账 2商家到账 3业务提现 4商家提现 5业务扣款 6商家扣款 7业务经理到账
        $data = monthStrtotime();
        $tx_money = Db::name('member_finance')->where([
            ['salesperson_id', '=', $this->salesperson_id],
            ['create_time', 'between', [$data[0], $data[1]]],
            ['type', '=', 3],
            ['is_count', '=', 2],
        ])->sum('money');
        $return_data['a'] = $this->salesperson_info['money'];
        $return_data['b'] = $tx_money;
        $return_data = $this->appEncrypt($return_data);
        msg(0, '查询成功', $return_data);
    }

    //余额提现
    public function memberTX()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        if ($data['money'] <= 0) {
            msg(-1, '输入正确值', '');
        }
        //1业务员到账 2商家到账 3业务提现 4商家提现 5业务扣款 6商家扣款 7业务经理到账
        $month_data = monthStrtotime();
        $tx_money = Db::name('member_finance')->where([
            ['salesperson_id', '=', $this->salesperson_id],
            ['create_time', 'between', [$month_data[0], $month_data[1]]],
            ['type', '=', 3],
            ['tx_type', '=', 1],
            ['is_count', '=', 2],
        ])->sum('money');
        $member_tx_money = config('site.yg_tx_money');
        //线上可提现金额
        $kt_money = $member_tx_money - $tx_money;
        if ($data['money'] > $kt_money) {
            $member_tx_fl = config('site.yg_tx_fl');
            if ($kt_money > 0) {
                //微信提现
                $member_finance_id = setMoney($this->memberId, $this->salesperson_id, 0, $kt_money, 2, 0, 0, 1, 3);
                //第三方提现
                $tx2_money = round($data['money'] - $kt_money, 2);
                $member_finance_id = setMoney($this->memberId, $this->salesperson_id, 0, $tx2_money, 2, 0, $member_tx_fl, 2, 3);
            } else {
                $member_finance_id = setMoney($this->memberId, $this->salesperson_id, 0, $data['money'], 2, 0, $member_tx_fl, 2, 3);
            }
        } else {
            $member_finance_id = setMoney($this->memberId, $this->salesperson_id, 0, $data['money'], 2, 0, 0, 1, 3);
        }
        msg(0, '提现成功', $member_finance_id);
    }

    //公告列表
    public function noticeList()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);

        $page = isset($data['page']) && $data['page'] > 0 ? (int)$data['page'] : 1;
        $length = isset($data['limit']) && $data['limit'] > 0 ? (int)$data['limit'] : 20;
        $where[] = ['is_del', '=', 0];
        $where[] = ['status', '=', 1];

        $data_list = Db::name('notice')
            ->where($where)
            ->order('create_time desc')
            ->paginate(
                $length, false, [
                    'page' => $page
                ]
            )->each(function ($item) {
                $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                return $item;
            });
        $return_data = $this->appEncrypt($data_list);
        msg(0, '查询成功', $return_data);
    }

    //获取通知详情
    public function noticeDetail()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        $notice_info = Db::name('notice')->find($data['notice_id']);
        $notice_info['create_time'] = date('Y-m-d H:i:s', $notice_info['create_time']);
        $return_data = $this->appEncrypt($notice_info);
        msg(0, '查询成功', $return_data);
    }

    //未读消息
    public function wdMsg()
    {
        $num = Db::name('msg')->where([
            ['role', '=', 3],
            ['is_del', '=', 0],
            ['is_read', '=', 0],
            ['member_id', '=', $this->memberId],
        ])->count();
        msg(0, '查询成功', $num);
    }

    public function ql()
    {
        Db::startTrans();
        try {
            Db::name('msg')->where([
                ['role', '=', 3],
                ['is_del', '=', 0],
                ['member_id', '=', $this->memberId],
            ])->update([
                'is_read' => 1,
                'read_time' => time()
            ]);
            Db::commit();
            msg(0, '清除完成成功', '');
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //业务员聊天记录         //1用户 2商户 3平台
    public function saleMsgList1()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        $page = isset($data['page']) && $data['page'] > 0 ? (int)$data['page'] : 1;
        $length = isset($data['limit']) && $data['limit'] > 0 ? (int)$data['limit'] : 9999999;

        $where[] = ['notice_id', '=', 0];
        $where[] = ['role', 'in', [1, 3]];
        $where[] = ['obj_id', '=', $this->salesperson_id];
        $where[] = ['member_id', '=', $this->memberId];
        Db::startTrans();
        try {
            $data_list = Db::name('msg')
                ->where($where)
                ->page($page, $length)
                ->order('create_time')
                ->paginate(
                    $length, false, [
                        'page' => $page
                    ]
                )->each(function ($item) {
                    if ($item['role']==3){
                        $item['role']=2;
                    }
                    $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                    return $item;
                });
            Db::name('msg')->where($where)->update([
                'is_read' => 1,
                'read_time' => time()
            ]);
            $return_data = $this->appEncrypt($data_list);
            Db::commit();
            msg(0, '查询成功', $return_data);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //业务员联系平台发送消息
    // role 1用户 2商户 3平台
    public function sendMsgSale()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        Db::startTrans();
        try {
            $insert_id = sendMsg(1, $this->memberId, $data['content'], $this->salesperson_id);
            Db::commit();
            msg(0, '发送完成', $insert_id);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //获取通知发送消息列表
    public function saleMsgList2()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        $page = isset($data['page']) && $data['page'] > 0 ? (int)$data['page'] : 1;
        $length = isset($data['limit']) && $data['limit'] > 0 ? (int)$data['limit'] : 9999999;

        $where[] = ['notice_id', '=', $data['notice_id']];
        $where[] = ['role', 'in', [1, 3]];
        $where[] = ['obj_id', '=', $this->salesperson_id];
        $where[] = ['member_id', '=', $this->memberId];
        Db::startTrans();
        try {
            $data_list = Db::name('msg')
                ->where($where)
                ->page($page, $length)
                ->order('create_time')
                ->paginate(
                    $length, false, [
                        'page' => $page
                    ]
                )->each(function ($item) {
                    $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                    if ($item['role']==3){
                        $item['role']=2;
                    }
                    return $item;
                });
            Db::name('msg')->where($where)->update([
                'is_read' => 1,
                'read_time' => time()
            ]);
            $return_data = $this->appEncrypt($data_list);
            Db::commit();
            msg(0, '查询成功', $return_data);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //发送关于通知的消息
    public function send2MsgSale()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        Db::startTrans();
        try {
            $insert_id = sendMsg(1, $this->memberId, $data['content'], $this->salesperson_id, $data['notice_id']);
            Db::commit();
            msg(0, '发送完成', $insert_id);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //设置账户信息 手机号
    public function setSaleInfo()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        $update_info = array(
            'id' => $this->salesperson_id,
        );
        Db::startTrans();
        try {
            if (isset($data['name']) && $data['name']) {
                $update_info['name'] = $data['name'];
            }
            if (isset($data['phone']) && $data['phone']) {
                $update_info['phone'] = $data['phone'];
                Db::name('member')->update([
                    'id'=>$this->memberId,
                    'tx_phone'=>$data['phone']
                ]);
            }
            if (isset($data['avatar_img']) && $data['avatar_img']) {
                $update_info['avatar_img'] = $data['avatar_img'];
            }
            Db::name('salesperson')->update($update_info);
            Db::commit();
            msg(0, '设置完成', '');
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    public function shopList2(){
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        $date1 = getTimestamp(0);
        $date2 = getTimestamp();
        $date3 = monthStrtotime();

        $where[] = ['salesperson_id', '=', $this->salesperson_info['id']];
        $where[] = ['is_del', '=', 0];

        //search_type 1今日 2昨日 3本月 4累计
        switch ($data['search_type']) {
            case 1:
                $where[]=['create_time', 'between', [$date1[0], $date1[1]]];
                break;
            case 2:
                $where[]=['create_time', 'between', [$date2[0], $date2[1]]];
                break;
            case 3:
                $where[]=['create_time', 'between', [$date3[0], $date3[1]]];
                break;
            case 4:
                $a=1;
                break;
            default:
                $where[]=['create_time', 'between', [$date1[0], $date1[1]]];
        }
        $page = isset($data['page']) && $data['page'] > 0 ? (int)$data['page'] : 1;
        $length = isset($data['limit']) && $data['limit'] > 0 ? (int)$data['limit'] : 20;

        $data_list =ShopModel::where($where)
            ->order('create_time desc')
            ->paginate(
                $length, false, [
                    'page' => $page
                ]
            )->each(function ($item) {
                //工号
                $gh = Db::name('shop_gh')->where([
                    ['shop_id', '=', $item['id']]
                ])->column('gh');
                $gh = implode(',', $gh);
                $item['gh'] = $gh;
                return $item;
            });
        $return_data = $this->appEncrypt($data_list);
        msg(0, '查询成功', $return_data);
    }
    // 编辑门店备注
    public function editShopRemark(){
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        Db::startTrans();
        try {
            $update_data=Db::name('shop')->update([
                'id'=>$data['shop_id'],
                'remark'=>$data['remark'],
            ]);
            Db::commit();
            msg(0, '编辑成功', $update_data);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }
    //下属门店列表没订单
    public function shopListnOrder()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        //sort_type 1默认排序 2开卡倒序 3开卡正序 4库存倒序 5库存正序
        switch ($data['sort_type']) {
            case 1:
                $sort = 'create_time desc';
                break;
            case 2:
                $sort = 'order_num desc';
                break;
            case 3:
                $sort = 'order_num ';
                break;
            case 4:
                $sort = 'bk_num desc ';
                break;
            case 5:
                $sort = 'bk_num ';
                break;
            default:
                $sort = 'create_time desc';
        }
        if (isset($data['shop_name']) && $data['shop_name']) {
            $where[] = ['shop_name', 'like', '%' . $data['shop_name'] . '%'];
        }

        $page = isset($data['page']) && $data['page'] > 0 ? (int)$data['page'] : 1;
        $length = isset($data['limit']) && $data['limit'] > 0 ? (int)$data['limit'] : 20;


        $time_in=DataUtils::getRecentDaysRange();
        $shop_list_ids=Db::name('order')->where([
            ['create_time','between',[$time_in['start_timestamp'],$time_in['end_timestamp']]]
        ])->column('shop_id');
        $where[] = ['salesperson_id', '=', $this->salesperson_info['id']];
        $where[] = ['id', 'not in', $shop_list_ids];
        $where[] = ['status', 'in', [1,2]];
        $data_list =ShopModel::where($where)
            ->order($sort)
            ->paginate(
                $length, false, [
                    'page' => $page
                ]
            )->each(function ($item) {
                //工号
                $gh = Db::name('shop_gh')->where([
                    ['shop_id', '=', $item['id']]
                ])->column('gh');
                $gh = implode(',', $gh);
                $item['gh'] = $gh;
                return $item;
            });
        $return_data = $this->appEncrypt($data_list);
        msg(0, '查询成功', $return_data);
    }
    //todo 购物车商品数量 1
    // 大类列表 1
    // 商品大类搜索 1
    // 优惠多少钱 1
    // 个人信息返回 是否商家 几星 是游客 还是商家 1
    // 商家申请后 是一级商家 游客购买不再升级 商家身份才升级 2
    // 不是商家就是游客 商家分 1-3星 2
    // 卡板导入 2
    // 分权修改 设置
    // 客户沟通 商家业务员订单接口修改 后台修改
    // 银河商城助力返现比例设置
    // 青岛汽配修改 配合对接
    // 心之悦 代理
    // 青岛汽配 增加 修改接口 给前端讲逻辑，嘉号通信 修改首页 图片压缩 测试上传 下载整理代码发送，心之悦 客户沟通 增加分账角色

}